<template>
  <div class="container">

    <Header></Header>
     <!-- 中间LOGO -->
     <div class="centerLogo">
      <van-image
        width="134"
        height="45"
        src="//yanxuan.nosdn.127.net/static-union/164793255107785e.png"
      />
     </div>

    <!-- 表单 -->
    <div class="fromBox">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="phone"
            name="用户名"
            placeholder="请输入手机号"
            clearable
            :rules="[{ pattern, message: '请输入正确的手机号' }]"
          />
          <van-field
            v-model="password"
            clearable
            name="密码"
            placeholder="请输入密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          >
          </van-field>
        </van-cell-group>

        <div class="codeLoginBox">
          <span @click="toRegister" class="text">注册账号</span>
          <van-button class="toCodeLogin" type="default">使用验证码登录</van-button>
        </div>

        <div style="margin: 16px;">
          <van-button block type="danger" native-type="submit">
            登录
          </van-button>
        </div>

      </van-form>
    </div>

    <!-- 其他方式登录 -->
    <div @click="toLogin" class="footer">
      其他登录方式<van-icon name="arrow" />
    </div>

  </div>
</template>

<script lang="ts" setup>
import {ref} from 'vue'
import { showSuccessToast, showFailToast } from 'vant';
import { reqLogin } from '../../../api/center';
import { useRouter } from 'vue-router';
import { setToken } from '../../../utils/token';


const phone = ref()
const pattern = /^1[3456789]\d{9}$/
const password = ref()
const router = useRouter()


const onSubmit =async () => {
  
  try {
  let res = await reqLogin({
    phone:phone.value,
    password:password.value
  })
  
  setToken(res.data.token)
  showSuccessToast('登录成功');
  router.push('/center')
  } catch (error) {
    showFailToast('账号或密码错误,请重新输入');
  }
  
}

const toLogin = () => {
  router.push('/login')
}

const toRegister = () => {
  router.push('/register')
}

</script>

<script lang="ts">
export default {
  name: 'phone'
}
</script>

<style lang="scss" scoped>
.container{
  .centerLogo{
    text-align:center;
    margin:28px 0;
  }
  .fromBox{
    padding: 0 5px;
    .codeLoginBox{
      display:flex;
      justify-content:space-between;
      align-items:center;
      margin-top:20px;
      .text{
        font-size:14px;
        margin-left:20px;
        color:#888;
      }
      .toCodeLogin{
        border:none;
        margin-right:5px;
      }
    }
    .checkBox{
      display:flex;
      align-items: flex-start;
      padding:0 16px;
      .agreement{
        font-size:14px;
        margin-left:10px;
        & a{
          color:#409EFF;
        }
      }
    }
    .van-cell{
      padding:10px 0;
      border-bottom: 1px solid #ddd;
      width:100%;
    }
  }
  .footer{
    font-size: 15px;
    display: flex;
    justify-content: center;
    margin-top: 80px;
  }
}

</style>
